﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CRM.Business
{
    public class WebIntent
    {
        public static bool AddWebIntent(CRM.DataLayer.WebIntent WI)
        {
            try
            {
                using (var db = new CRM.DataLayer.CRMDateEntities())
                {
                    db.AddToWebIntent(WI);
                    int changes = db.SaveChanges();
                    if (changes > 0)
                        return true;
                    else
                        return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 返回未通过审核的分页的订单信息
        /// </summary>
        /// <returns></returns>
        public static IList<DataLayer.WebIntent> PageListWebIntent(out int totleCount, int pageNum, int pageSize)
        {
            try
            {
                using (var db = new CRM.DataLayer.CRMDateEntities())
                {
                    var query = from p in db.WebIntent
                                orderby p.visitTime descending
                                select p;
                    List<CRM.DataLayer.WebIntent> result = new List<DataLayer.WebIntent>();
                    foreach (var item in query)
                    {
                        item.dementInfor = ContentInfo.CutString(item.dementInfor, 40);
                        result.Add(item);
                    }

                    //获取总记录数目
                    totleCount = result.ToArray().Length;
                    //获取请求页的最大索引
                    int max = (result.ToArray().Length <= pageNum * pageSize) ? result.ToArray().Length : pageNum * pageSize;
                    if (max < (pageNum - 1) * pageSize)
                    {
                        //请求页的最大索引比它上一页的最大索引还小：矛盾
                        return null;
                    }
                    else if (max < pageSize * pageNum)
                    {
                        //请求页是左后一页
                        return result.Skip((pageNum - 1) * pageSize).ToList();
                    }
                    else
                    {
                        //请求页正常返回
                        return result.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
                    }
                }
            }
            catch (Exception)
            {
                totleCount = 0;
                return null;
            }
        }
        /// <summary>
        /// 获得通过审核的订单
        /// </summary>
        /// <returns></returns>
        public static int GetDisposeCount()
        {
            try
            {
                using (var db = new CRM.DataLayer.CRMDateEntities())
                {
                    var reslut = from p in db.WebIntent
                                 where p.isDispose == true
                                 select p;
                    if (reslut != null)
                    {
                        return reslut.Count();
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
            catch (Exception)
            {
                return 0;
            }
        }
        /// <summary>
        /// 根据id号获取一条网上订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static DataLayer.WebIntent GetWebIntentById(int id)
        {
            try
            {
                using (var db = new CRM.DataLayer.CRMDateEntities())
                {
                    var reslut = db.WebIntent.SingleOrDefault(p => p.ID == id);
                    if (reslut != null)
                    {
                        return reslut;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }

        }
        /// <summary>
        /// 删除网上订单
        /// </summary>
        /// <param name="message">留言实体参数</param>
        /// <returns>如果操作成功返回true;反之，返回false</returns>
        public static bool DeleteWebIntent(DataLayer.WebIntent WI)
        {
            try
            {
                using (var db = new CRM.DataLayer.CRMDateEntities())
                {
                    var result = db.WebIntent.SingleOrDefault(p => p.ID == WI.ID);

                    if (result != null)
                    {
                        db.DeleteObject(result);
                        int changes = db.SaveChanges();
                        if (changes > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 处理网上订单
        /// </summary>
        /// <param name="message">留言实体参数</param>
        /// <returns>如果操作成功返回true;反之，返回false</returns>
        public static bool PostWebIntent(DataLayer.WebIntent WI)
        {
            try
            {
                using (var db = new DataLayer.CRMDateEntities())
                {
                    var result = db.WebIntent.SingleOrDefault(p => p.ID == WI.ID);
                    if (result != null)
                    {
                        result.isDispose = WI.isDispose;
                        int changes = db.SaveChanges();
                        if (changes > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }

            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
